#include "ULUI_LookupSingle.h"
#include "ULUI_UpdateBin.h"
#include "ULGfx_History.h"
#include "UL_Global.h"
#include "UL_Tools.h"

#define UPDATE_BIN_START_X	100
#define UPDATE_BIN_START_Y	100

ULUI_UpdateBin ULUI_UpdateBin::updateBin;

enum BinLookupIDs{
	EXIT,	
};	

static char *menuTitles[] = {
	"返回"
};

void ULUI_UpdateBin::Init()
{
	int i;
	VSDL_Point currentXY(UPDATE_BIN_START_X, UPDATE_BIN_START_Y);
	int w, h;
	ULUI_Menu menu;
	int ret;
	ULG_History *ulgHistory = ULG_History::Instance();

	AttachEventHandler(this);


	menu.ShowBoarder(true);
	menu.SetFont(UL_Global::UnionLottoFont());
	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}

	menus.SetFirstMenuXY(VSDL_Point(UPDATE_BIN_START_X, UPDATE_BIN_START_Y));
	menu.GetTitleSize(w, h);
	
	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	UL_Global::UnionLottoShowTip("获取txt文件...", currentXY);
	UL_Global::UnionLottoUpdateScreen();
	currentXY.y += h;
       	UL_Global::UnionLottoShowTip("对比更新bin文件...", currentXY);
	UL_Global::UnionLottoUpdateScreen();
	currentXY.y += h;
	ret = ulgHistory->UpdateBinHistory("history.txt", "history.bin");

	if (ret < 0){
       		UL_Global::UnionLottoShowTip("更新bin文件失败", currentXY);
		UL_Global::UnionLottoUpdateScreen();
		currentXY.y += h;
		updateResult = false;
		return ;	
	}

       	UL_Global::UnionLottoShowTip("更新bin文件成功", currentXY);
	UL_Global::UnionLottoUpdateScreen();
	currentXY.y += h;
	updateResult = true;
}

void ULUI_UpdateBin::CleanUp()
{
	menus.Reset();
}

void ULUI_UpdateBin::Pause()
{
}

void ULUI_UpdateBin::Resume()
{
	selectedMenu = -1;
}

void ULUI_UpdateBin::Update(VSDL_GameEngine *game)
{
	if (selectedMenu == -1){
		return ;
	}

	switch (selectedMenu){
		case EXIT:
			game->PopState();
			break;
	}
}

void ULUI_UpdateBin::Draw(VSDL_GameEngine *game)
{
	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(game->GetSurface());

	int fontHeight = UL_Global::UnionLottoGetFontHeight();

	if (updateResult){
		UL_Global::UnionLottoShowTip("更新BIN文件成功，点击返回到上一级菜单", VSDL_Point(UPDATE_BIN_START_X, UPDATE_BIN_START_Y + fontHeight));
	}else{
		UL_Global::UnionLottoShowTip("更新BIN文件失败，点击返回到上一级菜单", VSDL_Point(UPDATE_BIN_START_X, UPDATE_BIN_START_Y + fontHeight));
	}
}

ULUI_UpdateBin *ULUI_UpdateBin::Instance()
{
	return &updateBin;
}


void __HANDLE_EVENT ULUI_UpdateBin::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	if (keyStates[SDLK_ESCAPE]){
		game->PopState();
	}
}

void __HANDLE_EVENT ULUI_UpdateBin::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	menus.UpdateMenuList(absX, absY);
}

void __HANDLE_EVENT ULUI_UpdateBin::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	int i;
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;
		}
	}
	
}

void __HANDLE_EVENT ULUI_UpdateBin::OnQuit(void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	game->Quit();
}
